<template>
  <div>
    <Row>
      <i-col :span="24" offset="2">
        <Tree :data="driverListXHS" show-checkbox ref="tree" @on-check-change="getSelectedNodes"></Tree>
      </i-col>
    </Row>
    <div class="xlinkModal-footer">
      <Button type="primary" @click="subDriver">确认派遣</Button>
    </div>
  </div>
</template>

<script>
  import {assignmentForDriverXhs, getFleetAndTruckList} from '@api/backEnd/fleetControl'
  import tablePage from '@components/table/tablePage.vue'
  import _ from 'lodash'

  export default {
    name: 'dispatch-driver',
    data () {
      let driverColumn = [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '用户名',
          key: 'login_name'
        }
      ]
      let driverListXHS = []
      let driverListData = []
      let checkDriverList = []
      let datas = {driverColumn, driverListData, checkDriverList, driverListXHS}
      return datas
    },
    computed: {},
    props: {
      currentMainOrder: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    methods: {
      getDirverListXHS () {
        getFleetAndTruckList().then(res => {
          if (+res.data.isSuccess === 1) {
            let driverListData = res.data.data
            let driveTree = []
            _.forEach(driverListData, (item) => {
              let fleetDriverList = item.driverList
              let fleetDriverTree = []
              _.forEach(fleetDriverList, (driverItem) => {
                let driverName = driverItem.loginName
                let truckNo = driverItem.truckNo
                let thisTitle = driverName
                if (truckNo) {
                  thisTitle += '--' + truckNo
                }
                fleetDriverTree.push({title: thisTitle, driverId: driverItem.id})
              })
              let fleetTree = {
                title: item.fleetName,
                expand: true,
                fleetId: item.fleetId,
                children: fleetDriverTree
              }
              driveTree.push(fleetTree)
            })
            this.driverListXHS = driveTree
          }
        })
      },
      getSelectedNodes () {
        let selectNodes = this.$refs.tree.getCheckedNodes()
        if (selectNodes.length > 0) {
          let driverList = _.filter(selectNodes, (item) => {
            return item.fleetId ? false : true
          })
          let driverListArr = []
          _.forEach(driverList, (item) => {
            driverListArr.push(item.driverId)
          })
          this.checkDriverList = driverListArr
        } else {
          this.checkDriverList = []
        }
      },
      subDriver (status) {
        if (this.checkDriverList.length < 1) {
          this.$Message.info('请选择司机')
          return
        }
        let modelData = {
          mainOrderNo: this.currentMainOrder.orderNo,
          driverList: this.checkDriverList
        }
        // let param = Object.assign({}, modelData)
        assignmentForDriverXhs(JSON.stringify(modelData)).then(res => {
          if (+res.data.isSuccess === 1) {
            this.$Message.info('派遣成功')
            this.$emit('close')
          } else {
            this.$Message.info(res.data.res)
          }
        })
      }
    },
    mounted () {
      this.getDirverListXHS()
    },
    components: {tablePage}
  }
</script>

<style scoped>

</style>
